﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeTableManagement.Entities;
using TimeTableManagement;

namespace TimeTableManagement.Controllers
{
    [Authorize]
    public class ScheduleController : Controller
    {
        private QLSVContext db = new QLSVContext();

        //
        // GET: /Default1/

        public ViewResult Index()
        {
            var tb_slot_room = db.Tb_Slot_Room.Include(t => t.Tb_Room).Include(t => t.Tb_Slot);
            return View(tb_slot_room.ToList());
        }

        //
        // GET: /Default1/Details/5

        public ViewResult Details(int id)
        {
            Tb_Slot_Room tb_slot_room = db.Tb_Slot_Room.Find(id);
            return View(tb_slot_room);
        }

        //
        // GET: /Default1/Create

        public ActionResult Create()
        {
            ViewBag.id_room = new SelectList(db.Tb_Room, "id", "name");
            ViewBag.id_slot = new SelectList(db.Tb_Slot, "id", "slot");
            return View();
        } 

        //
        // POST: /Default1/Create

        [HttpPost]
        public ActionResult Create(Tb_Slot_Room tb_slot_room)
        {
            if (ModelState.IsValid)
            {
                if (this.checkexistslot(tb_slot_room))
                {
                    db.Tb_Slot_Room.Add(tb_slot_room);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "Khung thoi gian nay da co xu kien!");
                }
            }

            ViewBag.id_room = new SelectList(db.Tb_Room, "id", "name", tb_slot_room.id_room);
            ViewBag.id_slot = new SelectList(db.Tb_Slot, "id", "slot", tb_slot_room.id_slot);
            return View(tb_slot_room);
        }
        
        //
        // GET: /Default1/Edit/5
 
        public ActionResult Edit(int id)
        {
            Tb_Slot_Room tb_slot_room = db.Tb_Slot_Room.Find(id);
            ViewBag.id_room = new SelectList(db.Tb_Room, "id", "name", tb_slot_room.id_room);
            ViewBag.id_slot = new SelectList(db.Tb_Slot, "id", "slot", tb_slot_room.id_slot);
            return View(tb_slot_room);
        }

        //
        // POST: /Default1/Edit/5

        [HttpPost]
        public ActionResult Edit(Tb_Slot_Room tb_slot_room)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tb_slot_room).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.id_room = new SelectList(db.Tb_Room, "id", "name", tb_slot_room.id_room);
            ViewBag.id_slot = new SelectList(db.Tb_Slot, "id", "slot", tb_slot_room.id_slot);
            return View(tb_slot_room);
        }
        /// <summary>
        /// Kiem tra xu kien da ton tai hay chua, kiem tra theo room va slot
        /// </summary>
        /// <param name="slot_room"></param>
        /// <returns></returns>
        private bool checkexistslot(Tb_Slot_Room slot_room)
        {
            int? id_room = slot_room.id_room;
            int? id_slot = slot_room.id_slot;
            DateTime date = slot_room.date;
            var m = db.Tb_Slot_Room
                .Where(g => g.id_room == id_room)
                .Where(k => k.id_slot == id_slot)
                .Where(t=>t.date.Equals(date));
            if (m.ToList().Count == 0) return true;
            return false;
        }
        //
        // GET: /Default1/Delete/5
 
        public ActionResult Delete(int id)
        {
            Tb_Slot_Room tb_slot_room = db.Tb_Slot_Room.Find(id);
            return View(tb_slot_room);
        }

        //
        // POST: /Default1/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Tb_Slot_Room tb_slot_room = db.Tb_Slot_Room.Find(id);
            db.Tb_Slot_Room.Remove(tb_slot_room);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}